<?php
namespace App\Services\Goods\Goods;

use App\Models\Goods\Goods;
use Illuminate\Support\Facades\DB;
/**
 * 商品 逻辑层
 * Class GoodsLogic
 * @package App\Services\Goods\Goods
 */
class GoodsLogic
{
    // 添加新的广告位
    /*内部根据id进行简单直接的db查询商品列表*/
//    public function dbGoodsList($data)
//    {
//        return $this->db()
//            ->where('id','in',$data['id'])
//            ->where('is_del','neq',2)
//            ->select();
//    }

    /*艺货后台发布积分商品*/
    public function addIntegralGoods($data): array
    {
        $data['number'] = $data['origin_number'];
        $data['status'] = 1;
        $imgUrls = explode(",", $data['imgs_urls']);
        !$data['profile_pic'] && $data['profile_pic'] = $imgUrls[0];
        //orther_price,orther_info,goods_attrs,search_fields,json形式没有默认值
        //Tp5中加了此字段'integral'表中没有此字段,
        try{
            $res = Goods::create($data);
        }catch(\Exception $e){
            return server_err(null, $e->getMessage());
        }
        return auto_return($res);
    }

    /*艺货后台修改积分商品*/
    public function updateIntegralGoods($data): array
    {
        $_where['id'] = $data['id'];
        $_where['is_del'] = 0;
        $info = Goods::where($_where)->first();
        $number = isset($data['number']) ? $data['number'] : $info['number'];
        $origin_number = isset($data['origin_number']) ? $data['origin_number'] : $info['origin_number'];
        if($number > $origin_number){
            return verify_err(null, '剩余数量不能大于原始数量');
        }
        DB::listen(function($query) {
            $bindings = $query->bindings;
            $sql = $query->sql;
            foreach ($bindings as $replace){
                $value = is_numeric($replace) ? $replace : "'".$replace."'";
                $sql = preg_replace('/\?/', $value, $sql, 1);
            }
            dd($sql);
        });
        $res = (new Goods())->fillable(['cate_id', 'user_id', 'title', 'integral', 'profile_pic', 'imgs_urls', 'imgs_ids', 'number', 'origin_number', 'order', 'desc'])->where($_where)->update($data);
        return auto_return($res, '成功修改该商品', '修改商品失败');
    }

//    /*后台获取积分商品列表数据*/
//    public function allGoodsList($data): array
//    {
//        $_where['is_del'] = 0;
//        $data['keywords'] && $_where['goods.title'] = ['like', '%' . $data['keywords'] . '%'];
//        $manageAppId = ApiBase::getAppId();
//        $_whereOr = $manageAppId == $data['app_id'] ? true : 'goods_category.`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR goods_category.is_public = 1';
//        $list = $this->model($this->goodsModel)
//            ->alias('goods')
//            ->field('goods.*,goods_category.name as cate_name')
//            ->join('goods_category', 'goods_category.id = goods.cate_id', 'LEFT')
//            ->where($_where)
//            ->where($_whereOr)
//            ->order('goods.id desc')
//            ->paginate(null, false, ['page'=>$data['page'], 'list_rows'=>$data['listRows']])
//            ->toArray();
//        return auto_return_page($list);
//    }
//
//    /*管理员批量操作（审核、下架）商品*/
//    public function batchOperaGoods($data, $operate): array
//    {
//        //左链接分类表，对app_id进行验证操作
//        $manageAppId = ApiBase::getAppId();
//        $_whereOr = $manageAppId == $data['app_id'] ? true : 'goods_category.`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR goods_category.is_public = 1';
//
//        $_where['goods.id'] = ['in',$data['id']];
//        $_where['goods.is_del'] = 0;
//
//        if($operate == 'manageBatchSoldOutGoods'){
//            $_where['goods.status'] = ['in',['1','4']];
//        }
//
//        $saveData['goods.status'] = $data['status'];
//        $res = $this->model($this->goodsModel)
//            ->alias('goods')
//            ->join('goods_category', 'goods_category.id = goods.cate_id', 'LEFT')
//            ->where($_where)
//            ->where($_whereOr)
//            ->update($saveData);
//
//        return auto_return($res);
//    }
//
//    /*管理员删除一个或多个商品*/
//    public function manageDelGoods($data): array
//    {
//
//        !is_array($data) && $data['id'] = [$data['id']];
//        $_where['goods.id'] = ['in',$data['id']];
//        $_where['goods.is_del'] = 0;
//        $saveData['goods.is_del'] = 1;
//
//        //左链接分类表，对app_id进行验证操作
//        $manageAppId = ApiBase::getAppId();
//        $_whereOr = $manageAppId == $data['app_id'] ? true : 'goods_category.`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR goods_category.is_public = 1';
//
//        $res = $this->db()
//            ->alias('goods')
//            ->join('goods_category', 'goods_category.id = goods.cate_id', 'LEFT')
//            ->where($_where)
//            ->where($_whereOr)
//            ->update($saveData);
//
//        return auto_return($res);
//    }
//    /*前台获取积分商品分页列表*/
//    public function frontGoodsList($data): array
//    {
//        $_where['goods.is_del'] = 0;
//        $_where['goods.status'] = 1;
//        $_where['goods.number'] = ['gt', 0];
//        $data['keywords'] && $_where['goods.title'] = ['like', '%' . $data['keywords'] . '%'];
//        $manageAppId = ApiBase::getAppId();
//        $_whereOr = $manageAppId == $data['app_id'] ? true : 'goods_category.`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR goods_category.is_public = 1';
//        $list = $this->model($this->goodsModel)
//            ->alias('goods')
//            ->field('goods.*,goods_category.name as cate_name')
//            ->join('goods_category', 'goods_category.id = goods.cate_id', 'LEFT')
//            ->where($_where)
//            ->where($_whereOr)
//            ->paginate(null, false, ['page'=>$data['page'], 'list_rows'=>$data['listRows']])
//            ->toArray();
//        return auto_return_page($list);
//    }
//
//    /*前台获取某积分商品的详情*/
//    public function frontGoodsInfo($data): array
//    {
//        $_where['goods.number'] = ['gt', 0];
//        $_where['goods.status'] = 1;
//        $_where['goods.is_del'] = 0;
//        $_where['goods.id'] = $data['id'];
//        $manageAppId = ApiBase::getAppId();
//        $_whereOr = $manageAppId == $data['app_id'] ? true : 'goods_category.`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR goods_category.is_public = 1';
//        $info = $this->model($this->goodsModel)
//            ->alias('goods')
//            ->field('goods.*,goods_category.name as cate_name')
//            ->join('goods_category', 'goods_category.id = goods.cate_id', 'LEFT')
//            ->where($_where)
//            ->where($_whereOr)
//            ->find();
//        return auto_return($info);
//    }

   
}